/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.*;

/**
 * @author Administrator
 */
public class OwnSkinMain extends Canvas implements CommandListener {

    private String DKeyPress = "1";
    private int defMenu = 0;
    private int selectedDraw;
    private OwnSkin _ownskin;
    int xSelected = 0;
    int ySelected = 0;
    private int menuSelected = 0;
    private int xHover = 0;
    private int yHover = 0;
    private int _hit = 0;
    private int _xpos = 150;
    private int _ypos = 123;
    private int _oldCont = 0;
    private Command exitCommand;
    private Command okCommand;
    private Alert alert;

    public OwnSkinMain(OwnSkin os) {
        this._ownskin = os;
    }

    protected void keyPressed(int keyCode) {

        System.out.println("keypres =" + chekDb());

        if (chekDb() < 5) {
            int gameAction = getGameAction(keyCode);
            switch (gameAction) {
                case UP:
                    DKeyPress = "[UP]";
                    break;
                case DOWN:
                    DKeyPress = "[DOWN]";
                    break;
                case LEFT:
                    DKeyPress = "[LEFT]";
                    break;
                case RIGHT:
                    DKeyPress = "[RIGHT]";
                    break;
                case FIRE:
                    DKeyPress = "[FIRE]";
                    break;
                case GAME_A:
                    DKeyPress = "[LEFT_UP]";
                    break;
                case GAME_B:
                    DKeyPress = "[RIGHT_UP]";
                    break;
                case GAME_C:
                    DKeyPress = "[LEFT_DOWN]";
                    break;
                case GAME_D:
                    DKeyPress = "[RIGHT_DOWN]";
                    break;
                default:
                    DKeyPress = "-";
                    break;
            }
        } else {
            switchDisplayable(null, getAlert());
        }

        switch (keyCode) {
            case -7:
                //_ownskin.exit();
                //DKeyPress = "[RIGHT_MENU]";
                _ownskin.exit();
                break;
            case -6:
                //restart button
                //DKeyPress = "[LEFT_MENU]";
                _hit = 0;
                repaint();
                break;
        }

        System.out.println(keyCode);
        repaint();
    }

    protected void paint(Graphics g) {
        setFullScreenMode(true);

        g.setColor(0);
//		g1.setColor(0x000D73);
        g.fillRect(0, 0, getWidth(), getHeight());


        if (_ownskin.mainMenuImage != null) {
            g.drawImage(_ownskin.mainMenuImage, 0, 0, Graphics.LEFT | Graphics.TOP);
        }
        //g.drawImage(_ownskin.mainMenuImage, getWidth()/2, getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);

        if (DKeyPress.length() > 1) {
            _hit++;
        }

        /*
        if(_hit==33 || _hit==99) {
        _ownskin.ding();
        }
         *
         */

        if (_hit > 5) {

            RMSCounter conter = new RMSCounter();
            conter.open();
            if (conter.isEmpty()) {
                _oldCont = 1;
                conter.writeRecord(String.valueOf(_oldCont));
                _hit = 0;
            } else if (!conter.isEmpty()) {
                _oldCont = conter.readRecords(1);
                _oldCont += 1;
                conter.updateRecord(1, String.valueOf(_oldCont));
                System.out.println("counter =" + _oldCont);
                _hit = 0;
            }

            conter.close();
            if (_oldCont >= 5) {
                switchDisplayable(null, getAlert());
            }
            //bunyi ting...
        }

        if (String.valueOf(_hit).length() < 2) {
            _xpos = 154;
            _ypos = 123;
        } else {
            _xpos = 150;
            _ypos = 123;
        }

        //draw angka
        g.setColor(0xFFFFFF);
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
        g.drawString(String.valueOf(_hit), _xpos, _ypos, Graphics.TOP | Graphics.LEFT);

        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        g.drawString("Exit", getWidth() - 2, getHeight() - 2, Graphics.BOTTOM | Graphics.RIGHT);

        g.drawString("Reset", 0, getHeight() - 2, Graphics.BOTTOM | Graphics.LEFT);

        DKeyPress = "-";
    }

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
        // write post-switch user code here
    }

    public Alert getAlert() {
        if (alert == null) {
            // write pre-init user code here
            alert = new Alert("INFO", "\"This is trial Version, can only be used 5 times. Please buy the full version\"", null, AlertType.INFO);
            alert.addCommand(getOkCommand());
            alert.addCommand(getExitCommand());
            alert.setCommandListener(this);
            alert.setTimeout(Alert.FOREVER);
            // write post-init user code here
        }
        return alert;
    }

    public Command getOkCommand() {
        if (okCommand == null) {
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);
            // write post-init user code here
        }
        return okCommand;
    }

    public Command getExitCommand() {
        if (exitCommand == null) {
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);
            // write post-init user code here
        }
        return exitCommand;
    }

    public Display getDisplay() {
        return Display.getDisplay(_ownskin);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == okCommand) {
            _ownskin.startApp();
        } else if (c == exitCommand) {
            _ownskin.destroyApp(true);
        }
    }

    private int chekDb() {
        int co = 0;
        RMSCounter conter = new RMSCounter();
        conter.open();
        if (conter.isEmpty()) {
            co = 0;
        } else if (!conter.isEmpty()) {
            co = conter.readRecords(1);
            System.out.println("counter =" + co);
        }
        conter.close();
        return co;
    }
}
